const myfun = {
  setText: (name) => {
    return name ? name.replace(/\s+/g, '').replace(/[\r\n]/g, '') : ''
  },
  setArea: (info) => {
    return info ? info.replace(/[\r\n]/g, '<br/>').replace(/[\n]/g, '<br/>').replace(/\s+/g, '') : ''
  },
  removeHTMLTag: (str) => {
    str = str.replace(/<\/?[^>]*>/g, '')// --去除HTML tag--
    str = str.replace(/[ | ]*\n/g, '\n')// --去除行尾空白--
    str = str.replace(/\n[\s| | ]*\r/g, '\n')// --去除多余空行--
    str = str.replace(/&nbsp;|\t/ig, '')// --去掉&nbsp;和全角空格和tab制表符--
    return str
  },
  // ----
  setBoxHeight: (objAll, objTop, objCur) => {
    setTimeout(() => {
      const allHeight = document.querySelector(objAll) ? document.querySelector(objAll).offsetHeight : 0
      const topHeight = document.querySelector(objTop) ? document.querySelector(objTop).offsetHeight : 0
      let showStyle = ''
      if (Number(allHeight) >= Number(topHeight)) showStyle = 'height: ' + Number(allHeight - topHeight) + 'px;'
      document.querySelector(objCur) && document.querySelector(objCur).setAttribute('style', showStyle)
    }, 400)
  },
  // ----
  getTopH: () => {
    return new Promise(function (resolve, reject) {
      setTimeout(() => {
        const headerHeight = document.querySelector('header') ? document.querySelector('header').offsetHeight : 0
        resolve(headerHeight)
      }, 400)
    })
  },
  getFotH: () => {
    return new Promise(function (resolve, reject) {
      setTimeout(() => {
        const footerHeight = document.querySelector('footer') ? document.querySelector('footer').offsetHeight : 0
        resolve(footerHeight)
      }, 400)
    })
  },
  setSectionH: () => {
    setTimeout(() => {
      const headerHeight = document.querySelector('header') ? document.querySelector('header').offsetHeight : 0
      const footerHeight = document.querySelector('footer') ? document.querySelector('footer').offsetHeight : 0
      let showStyle = ''
      if (Number(headerHeight) > 0 && footerHeight > 0) {
        showStyle = 'top: ' + headerHeight + 'px; bottom: ' + footerHeight + 'px;'
      } else if (Number(headerHeight) === 0) {
        showStyle = 'bottom: ' + footerHeight + 'px;'
      } else if (Number(footerHeight) === 0) {
        showStyle = 'top: ' + headerHeight + 'px;'
      }
      document.querySelector('div.xw-section') && document.querySelector('div.xw-section').setAttribute('style', showStyle)
    }, 400)
  },
  setScrollPadding: () => {
    setTimeout(() => {
      const headerHeight = document.querySelector('header') ? document.querySelector('header').offsetHeight : 0
      const footerHeight = document.querySelector('footer') ? document.querySelector('footer').offsetHeight : 0
      let showStyle = ''
      if (Number(headerHeight) > 0 && footerHeight > 0) {
        showStyle = 'padding: ' + headerHeight + 'px 0 ' + footerHeight + 'px;'
      } else if (Number(headerHeight) === 0) {
        showStyle = 'padding: 0 0 ' + footerHeight + 'px;'
      } else if (Number(footerHeight) === 0) {
        showStyle = 'padding: ' + headerHeight + 'px 0 0;'
      }
      document.querySelector('div.xw-scroll') && document.querySelector('div.xw-scroll').setAttribute('style', showStyle)
    }, 400)
  },
  setDragMargin: () => {
    setTimeout(() => {
      const headerHeight = document.querySelector('header') ? document.querySelector('header').offsetHeight : 0
      const footerHeight = document.querySelector('footer') ? document.querySelector('footer').offsetHeight : 0
      let showStyle = ''
      if (Number(headerHeight) > 0 && footerHeight > 0) {
        showStyle = 'margin-top: ' + headerHeight + 'px; margin-bottom: ' + footerHeight + 'px;'
      } else if (Number(headerHeight) === 0) {
        showStyle = 'margin-bottom: ' + footerHeight + 'px;'
      } else if (Number(footerHeight) === 0) {
        showStyle = 'margin-top: ' + headerHeight + 'px;'
      }
      document.querySelector('div.xw-drag') && document.querySelector('div.xw-drag').setAttribute('style', showStyle)
    }, 400)
  },
  // ----
  setImgToTop: () => {
    const footerHeight = document.querySelector('footer') ? document.querySelector('footer').offsetHeight : 0
    if (document.querySelector('img.mescroll-totop')) {
      let forStyle = 'bottom: ' + Number(footerHeight + 20) + 'px;'
      if (document.querySelector('.tabfix')) {
        const topW = document.querySelector('.tabfix a').offsetWidth
        const topH = document.querySelector('.tabfix a').offsetHeight
        const topR =  window.innerWidth - document.querySelector('.tabfix').offsetLeft - topW
        let tabStyle = ''
        if (document.querySelector('img.mescroll-fade-in')) tabStyle = 'bottom: ' + Number(footerHeight + topH + 30) + 'px;'
        document.querySelector('.tabfix').setAttribute('style', tabStyle)
        forStyle = 'width: ' + topW + 'px; height: ' + topH + 'px; right: ' + topR + 'px; bottom: ' + Number(footerHeight + 20) + 'px;'
      }
      document.querySelector('img.mescroll-totop').setAttribute('style', forStyle)
    }
  },
  setImgLazy (event) {
    const opts = Object.assign({
      imgurl: 'imgurl',
      lazyTag: 'xw-lazying',
      lazyAnim: 'mescroll-fade-in'
    }, arguments[1] || {})
    const domArr = event.querySelectorAll('[' + opts.imgurl + ']')
    const eventH = event.offsetHeight + event.scrollTop
    const len = domArr.length
    for (let i = 0; i < Number(len); i++) {
      const dom = domArr[i]
      if (dom.getAttribute(opts.lazyTag) !== 'true' && Number(dom.offsetTop) < Number(eventH)) {
        const imgurl = dom.getAttribute(opts.imgurl)
        const temp = new Image()
        temp.onload = function() {
          const imgurl = this.src
          const dom = this.dom
          const showClass = opts.lazyAnim
          showClass && dom.classList.add(showClass);
          if (dom.tagName === 'IMG') {
            dom.src = imgurl
          } else {
            dom.style.backgroundImage = 'url(' + imgurl + ')'
          }
          dom.removeAttribute(opts.imgurl)
          dom.removeAttribute(opts.lazyTag)
        }
        temp.onerror = function() { this.dom.removeAttribute(opts.lazyTag) }
        temp.onabort = function() { this.dom.removeAttribute(opts.lazyTag) }
        temp.src = imgurl
        dom.setAttribute(opts.lazyTag, 'true')
        temp.dom = dom
      }
    }
  },
  setScrollTop: (obj, callback) => {
    const forScrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop
    if (obj) obj.style.cssText = 'position: fixed; top: -' + forScrollTop + 'px; right: 0; left: 0;'
    callback && callback()
  },
  setIosToTop: () => { window.scrollTo(0, 0) },
  // ----
  isIndepth: (event, func, opts) => { event[func](opts) },
  openPage: (event, type, name, info) => { event.$router[type]({ name: name, query: info }) },
  forToast: (event, func, info, callback, location) => { event[func]({ type: 1, content: info, time: 3, callback: callback || '', position: location }) },
  onLoading: (event, func, info) => { event[func]({ type: 2, title: info }) },
  disLoading: (event, func) => { event[func]() },
  // ----
  setSearchRes (event, info, items, callback) {
    const forKey = arguments[4] || 'forSearch'
    const newObj = event.$Store.get(forKey) || {}
    if (items && Number(items.length) > 0) {
      items.forEach((item, index) => {
        if (info && info[index]) {
          newObj[item] = info[index]
        } else {
          delete newObj[item]
        }
      })
    }
    event.$Store.set(forKey, newObj, () => { callback && callback(newObj) })
  },
  // ----
  getTelCenter: (tel) => {
    const reg = /^(\d{3})\d{4}(\d{4})$/
    return tel.replace(reg, '$1****$2')
  },
  getTelEnd: (tel) => {
    const reg = /^(\d{7})\d{4}$/
    return tel.replace(reg, '$1****')
  },
  getDateFolder: () => {
    const curDate = new Date()
    const year = curDate.getFullYear()
    let month = curDate.getMonth() + 1
    month = (month < 10) ? ('0' + month) : month
    return String(year) + String(month)
  },
  getDateName: () => {
    const curDate = new Date()
    const random = Math.round(Math.random() * 100000000)
    const year = curDate.getFullYear()
    let month = curDate.getMonth() + 1
    let date = curDate.getDate()
    month = (month < 10) ? ('0' + month) : month
    date = (date < 10) ? ('0' + date) : date
    return String(year) + String(month) + String(date) + String(random)
  },
  getToday: () => {
    const curDate = new Date()
    const forTime = curDate.toLocaleTimeString('chinese', { hour12: false })
    const year = curDate.getFullYear()
    let month = curDate.getMonth() + 1
    let date = curDate.getDate()
    month = (month < 10) ? ('0' + month) : month
    date = (date < 10) ? ('0' + date) : date
    return String(year) + '-' + String(month) + '-' + String(date) + ' ' + String(forTime)
  },
  getNewDate: (options) => {
    let curDate = new Date()
    // ----
    let opts = { type: 1 }
    if (options && Number(Object.keys(options).length) > 0) Object.assign(opts, options)
    // ----
    let millSeconds = 0
    let rDate = curDate
    // ----
    let year = rDate.getFullYear()
    // ----
    if (opts.date) curDate = new Date(String(opts.date).replace(/-/g, '/'))
    switch (Number(opts.type)) {
      case 2: {
        const forDay = Number(opts.day) || 0
        millSeconds = Math.abs(curDate) + (forDay * 24 * 60 * 60 * 1000)
        rDate = new Date(millSeconds)
        year = rDate.getFullYear()
        break
      }
      default: {
        const forNum = Number(opts.num) || 1
        year = curDate.getFullYear() + forNum
        break
      }
    }
    // ----
    let month = rDate.getMonth() + 1
    if (month < 10) month = '0' + month
    let date = rDate.getDate()
    if (date < 10) date = '0' + date
    const forTime = rDate.toLocaleTimeString('chinese', { hour12: false })
    return String(year) + '-' + String(month) + '-' + String(date) + ' ' + String(forTime)
  },
  getDateShort: (date) => {
    const curDate = date ? new Date(date.replace(/-/g, '/')) : new Date()
    const year = curDate.getFullYear()
    let month = curDate.getMonth() + 1
    let day = curDate.getDate()
    month = (month < 10) ? ('0' + month) : month
    day = (day < 10) ? ('0' + day) : day
    return String(year) + '-' + String(month) + '-' + String(day)
  },
  getDateLimit: (date, callback) => {
    const amountDate = date ? new Date(date.replace(/-/g, '/')).getTime() : new Date().getTime()
    const curDate = new Date().getTime()
    const day = parseInt((curDate - amountDate) / (1000 * 60 * 60 * 24))
    callback && callback(day)
  },
  getJsonData: (item, callback) => {
    if (Object.prototype.toString.call(item) === '[object String]') {
      try {
        const curArr = JSON.parse(item)
        let newItems = []
        if (Object.prototype.toString.call(curArr) === '[object Array]') {
          newItems = curArr
        } else {
          newItems.push(curArr)
        }
        callback && callback(newItems)
      } catch (e) { return false }
    }
  },
  // ----
  appSource: (iosCallBack, andriodCallBack) => {
    const u = navigator.userAgent
    const isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)
    const isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1
    if (isIOS) iosCallBack && iosCallBack()
    if (isAndroid) andriodCallBack && andriodCallBack()
  }
}

export default myfun